package com.lsl.musicplayer.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * author lishilin
 * date 2023/8/7
 * desc 自定义View，负责绘制音乐节奏跳动控制UI
 */
public class VisualizerView extends View {

    private boolean isPlay = false;
    private Rect mRect1, mRect2, mRect3, mRect4, mRect5, mRect6, mRect7;
    private List<Rect> rects;
    private int top1, top2, top3, top4, top5, top6, top7;
    private Paint mForePaint = new Paint();
    private List<Paint> paints;
    private Canvas canvas;
    private final int rectNums = 40;

    public VisualizerView(Context context) {
        super(context);
        init();
    }

    public VisualizerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VisualizerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        rects = new ArrayList<>();
        paints = new ArrayList<>();
        canvas = new Canvas();
        if (rects != null && paints != null) {
            for (int i = 0; i < rectNums; i++) {
                Rect rect = new Rect(13 * i, 5 * i, 13 * i + 8, 120);
                rects.add(rect);
                Paint paint = new Paint();
                paints.add(paint);
            }
        }
//         mRect1 = new Rect(0, top1, 8, 100);
//         mRect2 = new Rect(10, top2, 18, 100);
//         mRect3 = new Rect(20, top3, 28, 100);
//         mRect4 = new Rect(30, top4, 38, 100);
//         mRect5 = new Rect(40, top5, 48, 100);
//         mRect6 = new Rect(50,top6,58,100);
//         mRect7 = new Rect(60,top7,68,100);
        mForePaint.setStrokeWidth(4.5f);
        mForePaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        canvas.drawRect(mRect1,mForePaint);
//        canvas.drawRect(mRect2,mForePaint);
//        canvas.drawRect(mRect3,mForePaint);
//        canvas.drawRect(mRect4,mForePaint);
//        canvas.drawRect(mRect5,mForePaint);
//        canvas.drawRect(mRect6,mForePaint);
//        canvas.drawRect(mRect7,mForePaint);
        if (rects != null) {
            for (int i = 0; i < rectNums; i++) {
                canvas.drawRect(rects.get(i), paints.get(i));
            }
        }
    }

    public void updateDraw() {
        Thread mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                Random random = new Random();
                while (isPlay) {
//                    top1 = random.nextInt(101);
//                    top2 = random.nextInt(101);
//                    top3 = random.nextInt(101);
//                    top4 = random.nextInt(101);
//                    top5 = random.nextInt(101);
//                    top6 = random.nextInt(101);
//                    top7 = random.nextInt(101);
//                    mRect1.top = top1;
//                    mRect2.top = top2;
//                    mRect3.top = top3;
//                    mRect4.top = top4;
//                    mRect5.top = top5;
//                    mRect6.top = top6;
//                    mRect7.top = top7;
                    if (rects != null && paints != null) {
                        for (int i = 0; i < rectNums; i++) {
                            rects.get(i).top = random.nextInt(101);
                            // 生成随机的 R、G、B 值
                            int red = random.nextInt(256);
                            int green = random.nextInt(256);
                            int blue = random.nextInt(256);
                            paints.get(i).setColor(Color.rgb(red, green, blue));
                        }
                    }

                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    invalidate();
                }
            }
        });
        if (isPlay) {
            mThread.start();
        } else {
            mThread.stop();
        }
    }

    public void setIsPlay(boolean play) {
        this.isPlay = play;
    }

}



